Description:
Calling an abstract method from an abstract class constructor
may result in execution of methods declared in subclasses before the initialization
of the object is completed.
Incorrect:
In the following example, an attempt to create a WordPair object
will result in a NullReferenceException being thrown.
public abstract class WordSequence {
protected int length;
protected WordSequence() {
computeLength();
}
protected abstract void computeLength();
}
public class WordPair : WordSequence {
private string first;
private string second;
public WordPair(string first, string second) {
this.first = first;
this.second = second;
}
protected override void computeLength() {
length = first.Length + second.Length;
}
}